import 'dart:convert';
import 'package:encrypt/encrypt.dart' as encrypt;
import 'package:base_flutter/base_flutter.dart';
import 'package:url_launcher/url_launcher.dart';

class SaasUtils{

  /// 调起拨号页
  static void callPhone(String phone) async {
    String url = 'tel:' + phone;
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      throw "拨号失败";
    }
  }

  static bool isCloudPrinter(String code){
    var regex = r"N[0-9A-Z]{12}";
    var reg =  RegExp(regex);
    return reg.hasMatch(code);
  }

  static String _PREFIX = "https://qr.yytong.com/";
  static String _KEY = "Yytong#2021@Axun";

  static Future<Map<String, String>?> decodeQrcode(String qrcode) async {
    var content = qrcode.replaceAll(_PREFIX, '');
    final key = encrypt.Key(Utf8Encoder().convert(_KEY));
    final iv = encrypt.IV.fromLength(16);
    var encrypter = encrypt.Encrypter(encrypt.AES(key,mode: encrypt.AESMode.ecb,));
    var data = encrypter.decrypt64(content,iv: iv);
    var list = data.split("/");
    if(list.length!=3){
      return null;
    }
    return {
      "type":list[1],
      "id":list[2]
    };
  }


}